package com.krain.ddbb.adapter;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.krain.corelibrary.widget.AnimCheckBox;
import com.krain.corelibrary.widget.recyleview.BaseRecyclerViewAdapter;
import com.krain.ddbb.R;
import com.krain.ddbb.base.APP;
import com.krain.ddbb.entity.WorkNameBean;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by ex-kuangxiaomin101 on 2015-12-25.
 */
public class UserWorkAdapter extends BaseRecyclerViewAdapter<UserWorkAdapter.ViewHolder> {

    private List<WorkNameBean> mList;
    private Context context;
    private OnItemCheckedChaged onItemCheckedChaged;
    private ArrayMap<String, Boolean> checkedMap;


    public UserWorkAdapter(List<WorkNameBean> mList, Context context, OnItemCheckedChaged onItemCheckedChaged) {
        this.mList = mList;
        this.context = context;
        this.onItemCheckedChaged = onItemCheckedChaged;
        checkedMap = new ArrayMap<>();
        ArrayList<WorkNameBean> beans = ((APP) context.getApplicationContext()).getmUserinfo().getWork();
        for (WorkNameBean b : mList) {
            checkedMap.put(b.getId(), false);
            for (WorkNameBean bs : beans) {
                if (bs.getId().equals(b.getId())) {
                    checkedMap.put(bs.getId(), true);
                }
            }

        }
        Logger.v(checkedMap.toString());
    }

    @Override
    public ViewHolder getViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public View getItemView(int viewType, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_work_list, parent, false);

    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.item_work_type.setText(mList.get(position).getWork_name());
        holder.item_work_checkbox.setEnabled(true);
        holder.item_work_checkbox.setOnCheckedChangeListener(null);
        if (checkedMap.get(mList.get(position).getId())) {
            holder.item_work_checkbox.setChecked(true);
        } else {
            holder.item_work_checkbox.setChecked(false);
        }
        holder.item_work_checkbox.setOnCheckedChangeListener(new OnCheckedChanged(position, mList.get(position).getId()));
    }


    class OnCheckedChanged implements CompoundButton.OnCheckedChangeListener {
        int position;
        String id;

        public OnCheckedChanged(int position, String id) {
            this.position = position;
            this.id = id;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checkedMap.put(id, isChecked);
        }
    }

    public List<String> getChecked() {
        List<String> ids = new ArrayList<>();
        for (String s : checkedMap.keySet()) {
            if (checkedMap.get(s))
                ids.add(s);
        }
        return ids;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView item_work_type;
        public AnimCheckBox item_work_checkbox;

        public ViewHolder(View itemView) {
            super(itemView);
            item_work_type = (TextView) itemView.findViewById(R.id.act_work_item_tv_type);
            item_work_checkbox = (AnimCheckBox) itemView.findViewById(R.id.act_work_item_checkbox);
        }
    }


    public interface OnItemCheckedChaged {
        public void onCheckedChaged(String id, boolean isChecked);
    }
}
